package com.bac.bacplatform;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.bac.javabeans.Principal;
import com.bac.utils.Config;
import com.bac.utils.Storage;
import com.bac.utils.Util;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MoveInActivity extends BaseActivity implements OnClickListener {

	private RelativeLayout choose1;
	private RelativeLayout choose2;
	private ImageView chooseimage1;
	private ImageView chooseimage2;
	private EditText money;
	private Button pay;
	private RelativeLayout movein_return;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_move_in);
	}

	@Override
	public void init() {
		money = (EditText) findViewById(R.id.movein_money_text);
		movein_return = (RelativeLayout) findViewById(R.id.movein_return);
		chooseimage1 = (ImageView) findViewById(R.id.movein_choose_check1);
		chooseimage2 = (ImageView) findViewById(R.id.movein_choose_check2);
		choose1 = (RelativeLayout) findViewById(R.id.movein_choosepayfunction_layout1);
		choose2 = (RelativeLayout) findViewById(R.id.movein_choosepayfunction_layout2);
		pay = (Button) findViewById(R.id.movein_payok_btn);
		choose1.setOnClickListener(this);
		choose2.setOnClickListener(this);
		movein_return.setOnClickListener(this);
		pay.setOnClickListener(this);
		money.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				if (money.getText().toString().length() > 0) {
					if (Integer.valueOf(money.getText().toString()) > 10000) {
						money.setText("10000");
					}
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {

			}
		});
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.movein_choosepayfunction_layout1:
			chooseImgChange(1);
			break;
		case R.id.movein_choosepayfunction_layout2:
			chooseImgChange(2);
			break;
		case R.id.movein_return:
			finish();
			break;
		case R.id.movein_payok_btn:
			pay();
			break;
		default:
			break;
		}
	}

	private void chooseImgChange(int i) {
		chooseimage1.setImageResource(R.drawable.movein_choose_no);
		chooseimage2.setImageResource(R.drawable.movein_choose_no);
		switch (i) {
		case 1:
			chooseimage1.setImageResource(R.drawable.movein_choose_ok);
			break;
		case 2:
			chooseimage2.setImageResource(R.drawable.movein_choose_ok);
			break;
		default:
			break;
		}
	}

	public void pay() {
		
		if (money.getText().toString().length() == 0) {
			showToast("请输入转入金额");
			return;
		}
		
		/**
		 * 成功返回结果
		 */
		Listener<String> sL = new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {

				dismissLoadingProgressNow();
				Log.i("TAG----success", response.toString());
				HashMap<String, String> result = new Gson().fromJson(response
						.toString().trim(),
						new TypeToken<HashMap<String, String>>() {
						}.getType());

				if (result != null && result.containsKey("paymentUrl")) {
					Intent intentToPay = new Intent(MoveInActivity.this,
							YeepayActivity.class);
					intentToPay
							.putExtra("paymentUrl", result.get("paymentUrl"));
					startActivityIn(intentToPay);
				}
			}
		};

		showLoadingProgress();
		String path = Config.URL_YEEPAY;
		Map<String, String> map = new HashMap<String, String>();
		map.put("amount", money.getText().toString());
		map.put("terminalId", Util.getDeviceID(MoveInActivity.this));
		map.put("transactionCode", "GAS003");
		Util.httpStringPost(path, sL, this, map);
	}

}
